package com.spring.sunflower.util;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.alipay.sdk.app.PayTask;
import com.blankj.utilcode.util.ToastUtils;
import com.spring.sunflower.bean.WeChatPayBean;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.Map;


/**
 * Created by Kevin on 2021/8/26<br/>
 * <p>
 * Describe:<br/>
 */
public class PayUtils {
    private static PayUtils mInstance;
    private static Activity mContext;
    private static final int AliPay_FLAG = 1;
    private static String TAG = "PayUtils";
    private IWXAPI wxapi;

    public static PayUtils getInstance(Activity context) {
        mContext = context;
        if (mInstance == null) {
            synchronized (PayUtils.class) {
                if (mInstance == null) {
                    mInstance = new PayUtils();
                }
            }
        }
        return mInstance;
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case AliPay_FLAG:
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    String result = payResult.getResult();
                    String resultStatus = payResult.getResultStatus();
                    LogUtils.INSTANCE.logD(TAG, "result=" + result + ",status=" + resultStatus);
                    if (resultStatus.equals("9000")) {
                        ToastUtils.showShort("支付成功");
                        if (listener != null) {
                            listener.onPaySuccess();
                        }
                    } else if (TextUtils.equals(resultStatus, "8000")) {
                        ToastUtils.showShort("正在处理中");

                    } else if (TextUtils.equals(resultStatus, "4000")) {
                        ToastUtils.showShort("订单支付失败");

                    } else if (TextUtils.equals(resultStatus, "6001")) {
                        ToastUtils.showShort("支付取消");
                    } else {
                        ToastUtils.showShort("支付失败");
                    }
                    break;
            }
        }
    };

    public void payWithAlipay(String orderId) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                PayTask payTask = new PayTask(mContext);
                Map<String, String> map = payTask.payV2(orderId, true);
                Message msg = new Message();
                msg.what = AliPay_FLAG;
                msg.obj = map;
                mHandler.sendMessage(msg);
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

    public void payWithWeChatPay(WeChatPayBean.DatasBean bean) {
        wxapi = WXAPIFactory.createWXAPI(mContext, bean.getAppid(), false);
        wxapi.registerApp(bean.getAppid());
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                PayReq payReq = new PayReq();
                payReq.appId = bean.getAppid();
                payReq.partnerId = bean.getPartnerid();
                payReq.prepayId = bean.getPrepayid();
                payReq.packageValue = bean.getPackageX();
                payReq.nonceStr = bean.getNoncestr();
                payReq.timeStamp = bean.getTimestamp();
                payReq.sign = bean.getSign();
                wxapi.sendReq(payReq);
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

    private OnPayResultListener listener;

    public void setOnPayResultListener(OnPayResultListener l) {
        listener = l;
    }

    public interface OnPayResultListener {
        void onPaySuccess();
    }
}

